package com.cnap.controller;

import com.cnap.CnapApplicationTests;
import com.cnap.model.application.ApplicationDTO;
import com.cnap.model.application.QueryApplicationParam;
import com.cnap.model.application.UpdateApplicationParam;
import com.cnap.model.common.PageResult;
import com.cnap.model.log.OperationTypeEnum;
import com.cnap.service.ApplicationService;
import com.cnap.service.OperationLogService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class ApplicationControllerTest extends CnapApplicationTests {

    @Autowired
    private ApplicationController applicationController;

    @MockBean
    private ApplicationService applicationServiceMock;
    @MockBean
    private OperationLogService operationLogServiceMock;

    @Test
    void listApplication() {
        QueryApplicationParam param = new QueryApplicationParam();
        Mockito.when(applicationServiceMock.findByPage(param)).thenReturn(new PageResult<>());

        Assertions.assertDoesNotThrow(()->{
            applicationController.listApplication(param);
        });
    }

    @Test
    void uploadApplication() {
        ApplicationDTO applicationDTO = new ApplicationDTO();
        Mockito.when(applicationServiceMock.uploadApplication(applicationDTO)).thenReturn("id");

        Assertions.assertDoesNotThrow(()->{
            applicationController.uploadApplication(applicationDTO);
        });
    }

    @Test
    void updateApplication() {
        UpdateApplicationParam param = new UpdateApplicationParam();
        Mockito.when(applicationServiceMock.updateApplication(param)).thenReturn(true);

        Mockito.when(operationLogServiceMock.saveOperationLog(OperationTypeEnum.OTHER.getType(), "admin更新了应用")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            applicationController.updateApplication(param);
        });
    }

    @Test
    void deleteApplication() {
        Mockito.when(applicationServiceMock.deleteApplicationById("appId")).thenReturn(true);

        Mockito.when(operationLogServiceMock.saveOperationLog(OperationTypeEnum.DELETE.getType(), "admin删除了应用")).thenReturn(true);

        Assertions.assertDoesNotThrow(()->{
            applicationController.deleteApplication("appId");
        });
    }

    @Test
    void listApplicationForSplit() {
        QueryApplicationParam param = new QueryApplicationParam();
        Mockito.when(applicationServiceMock.findByPageForSplit(param)).thenReturn(new PageResult<>());

        Assertions.assertDoesNotThrow(()->{
            applicationController.listApplicationForSplit(param);
        });
    }
}